//
//  TopDelinquentCustomersViewController.m
//  CollectionManagerFlow
//
//  Created by Sophia Tsay on 5/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "TopDelinquentCustomersViewController.h"
#import "CollectorsListTableViewController.h"

@interface TopDelinquentCustomersViewController ()

@end

@implementation TopDelinquentCustomersViewController
@synthesize topPopoverDeleage = _topPopoverDeleage;
@synthesize objectType = _objectType;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    if ([self.objectType isEqualToString:@"Customer"]) {
        //numberOfRows = [topDelinquentCustomers count];
        numberOfRows = [delinquentCustomerValues count];
    } else if ([self.objectType isEqualToString:@"Collector"]) {
        numberOfRows = [defaultCollectorsList count];
    }
    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Top Delinquent Customers Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    if ([self.objectType isEqualToString:@"Customer"]) {
        //cell.textLabel.text = [topDelinquentCustomers objectAtIndex:indexPath.row];
        cell.textLabel.text = [[delinquentCustomerValues objectAtIndex:indexPath.row] objectAtIndex:0];
        cell.detailTextLabel.text = [[[@"Balance: " stringByAppendingString:[[delinquentCustomerValues objectAtIndex:indexPath.row] objectAtIndex:2]] stringByAppendingString:@"; Overdue: "] stringByAppendingString:[[delinquentCustomerValues objectAtIndex:indexPath.row] objectAtIndex:1]];
    } else if ([self.objectType isEqualToString:@"Collector"]) {
        cell.textLabel.text = [defaultCollectorsList objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = @"CEI: 87.21, Number of Delinquencies: 6, Number of Customers: 18";
    }        
    cell.backgroundColor = [UIColor darkGrayColor];
    cell.textLabel.textColor = [UIColor lightGrayColor];
    cell.detailTextLabel.textColor = [UIColor whiteColor];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 2;
    [cell.textLabel sizeToFit];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    NSString *tableTitle;
    if ([self.objectType isEqualToString:@"Customer"]) {
        tableTitle = @"Top Delinquent Customers";
    } else if ([self.objectType isEqualToString:@"Collector"]) {
        tableTitle = @"Collectors";        
    }
    return tableTitle;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate
#define CUSTOMER_DEFAULT_NAME @"customer_default_name"

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    if ([self.objectType isEqualToString:@"Customer"]) {
        [defaults setObject:cell.textLabel.text forKey:CUSTOMER_DEFAULT_NAME];        
    } else if ([self.objectType isEqualToString:@"Collector"]) {
        [defaults setObject:cell.textLabel.text forKey:COLLECTOR_DEFAULT_NAME];        
    }
    [defaults synchronize];
    [self.topPopoverDeleage dismissAtTap];

}

@end
